import {ref} from 'vue'
import {defineStore} from 'pinia'
import {getUser} from '@/utils/local'
import {isObject} from '@/utils/type'
import {USER_INFO_STORE} from '@/utils/constant'

const userStore = defineStore(USER_INFO_STORE, () => {
  const userInfo = ref(getUser())

  function updateUser(user) {
    if (!isObject(user)) {
      throw new Error(`params must be an object, not ${user}`)
    }

    userInfo.value = user
  }

  return {
    userInfo, updateUser
  }
})

export default userStore